package cn.lsh.web.controller;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.lsh.pojo.User;
import cn.lsh.service.UserService;

@Controller
public class LoginController {

	@Autowired
	private UserService userService;
	
	@RequestMapping("/login")
	public String login(){
System.out.println("************login**************");
		return "login";
	}
	
	@RequestMapping("/loginUser")
	public String loginUser(String username,String password,HttpSession session){
System.out.println("***********loginUser*************");
		UsernamePasswordToken token=new UsernamePasswordToken(username,password);
		Subject subject=SecurityUtils.getSubject();
		
		List<User> users=userService.findUsers();
		try {
			subject.login(token);//完成登录
			User user=(User) subject.getPrincipal();
			session.getServletContext().setAttribute("users", users);
			session.setAttribute("user", user);
			return "index";
		} catch (Exception e) {
			// TODO: handle exception
			System.out.println(e.getMessage());
			return "login";
		}
		
	}
	
	@RequestMapping("/logut")
	public String loginout(HttpSession session){
		Subject subject=SecurityUtils.getSubject();
		subject.logout();
		return "login";
	}
	
	@RequestMapping("/el")
	public String testEl(HttpServletRequest request){
//		request.setAttribute("username", "lsh");
		return "el";
	}
	
	@RequestMapping("/e")
	public String reirect(HttpServletRequest request){
		request.setAttribute("username", "zzq");
//		return "redirect:jsp/el.jsp";
		return "forward:jsp/el.jsp";
	}
}
